<!--组织管理-->
<template>
	<el-card>
		<el-form :inline="true" :model="state.queryForm" @keyup.enter="getDataList()">
			<el-form-item class="form-button-right">
				<el-button v-auth="'module:organization:save'" type="primary" @click="addOrUpdateHandle()">新增组织</el-button>
				<el-button type="danger" @click="toggleExpandAll()">
					<el-icon style="width: 100%"> <Sort />更多操作</el-icon>
				</el-button>
			</el-form-item>

			<el-form-item>
				<KeyQuery v-model="state.queryForm.keyword" @getDataList="getDataList()" />
			</el-form-item>
		</el-form>

		<el-table
			v-if="refreshTable"
			v-loading="state.dataListLoading"
			:default-expand-all="isExpandAll"
			:data="state.dataList"
			row-key="id"
			border
			style="width: 100%"
		>
			<el-table-column prop="abbreviation" label="组织简称" header-align="center" width="150"></el-table-column>
			<el-table-column prop="code" label="组织编码" header-align="center" align="center"></el-table-column>
			<el-table-column prop="typeName" label="组织类型" header-align="center" align="center">
				<template #default="scope"> {{ organTypeMap.get(scope.row.typeId) }}</template>
			</el-table-column>

			<el-table-column prop="superiorPrincipal" label="组织负责人" header-align="center" align="center"></el-table-column>

			<el-table-column prop="population" label="工作人数" header-align="center" align="center"></el-table-column>
			<el-table-column prop="population" label="人员编制" header-align="center" align="center"></el-table-column>
			<el-table-column prop="isEnable" label="组织状态" header-align="center" align="center">
				<template #default="scope">
					<el-tag v-if="scope.row.isEnable === 0" type="info">禁用</el-tag>
					<el-tag v-if="scope.row.isEnable === 1" type="success">启用</el-tag>
				</template>
			</el-table-column>

			<el-table-column prop="description" label="组织描述" header-align="center" align="center" v-if="false"></el-table-column>
			<el-table-column label="操作" fixed="right" header-align="center" align="center" width="250">
				<template #default="scope">
					<el-button v-auth="'module:organization:update'" type="primary" link @click="addSubset(scope.row.id)">添加下级组织</el-button>
					<el-button v-auth="'module:organization:update'" type="primary" link @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
					<el-button v-auth="'module:organization:delete'" type="primary" link @click="deleteBatchHandle(scope.row.id)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!-- 弹窗, 新增 / 修改 -->
		<add-or-update ref="addOrUpdateRef" @refreshDataList="getDataList" :typeList="organTypeList"></add-or-update>
	</el-card>
</template>

<script setup lang="ts" name="ModuleOrganizationIndex">
import { useCrud } from '@/hooks'
import { reactive, ref, nextTick, Ref } from 'vue'
import { IHooksOptions } from '@/hooks/interface'
import { Sort } from '@element-plus/icons-vue'

import AddOrUpdate from './plane/organ-a-u.vue'

import { findListByDictId } from '@/api/nb/business/dictionaries_subset'

const state: IHooksOptions = reactive({
	dataListUrl: '/nb/organization/list',
	deleteUrl: '/nb/organization/delete',
	isPage: false
})

//新增页面
const addOrUpdateRef = ref()
const addOrUpdateHandle = (id?: number) => {
	addOrUpdateRef.value.init(id)
}

//添加下级页面
const addSubset = (id?: number) => {
	addOrUpdateRef.value.pid = id
	addOrUpdateRef.value.init()
}

// 是否展开，默认全部折叠
const isExpandAll = ref(false)
// 是否重新渲染表格状态
const refreshTable = ref(true)

/**
 * 切换 展开/折叠
 */
const toggleExpandAll = () => {
	refreshTable.value = false
	isExpandAll.value = !isExpandAll.value
	nextTick(() => {
		refreshTable.value = true
	})
}

//列表树
const organTypeList = ref([])
const organTypeMap = ref(new Map())
const getDictList = (code: String, list: Ref) => {
	findListByDictId({ params: { code: code } }).then(res => {
		list.value = res.data
		list.value.forEach((e: { id: any; name: any }) => {
			organTypeMap.value.set(e.id, e.name)
		})
	})
}
getDictList('101', organTypeList)

const { getDataList, deleteBatchHandle } = useCrud(state)
</script>
